package com.wanter.common.utils.password;

import com.wanter.common.utils.ID.IDutils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * 生成随即密码的工具类
 * Created by er on 17-8-9.
 */
public class PassWordUtil {
    /**
     * 对密码加密程序
     * @param password password
     * @return 加密后的结果
     */
    public static String toMD5(String password){
        String reStr = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(password.getBytes());
            StringBuffer stringBuffer = new StringBuffer();
            for (byte b : bytes){
                int bt = b&0xff;
                if (bt < 16){
                    stringBuffer.append(0);
                }
                stringBuffer.append(Integer.toHexString(bt));
            }
            reStr = stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return reStr;
    }

    /**
     * 得到随即秘密
     * @return 根据时间戳得到随即密码
     */
    public static String getRandomPassword(){
        return PassWordUtil.toMD5(String.valueOf(IDutils.genItemId()));
    }

    /**
     * 根据uuid来生存随即密码
     * @return 密码
     */
    public static String getUUIDPassword(){
        return PassWordUtil.toMD5(IDutils.getUUID());
    }





















    public static void main(String[] args) {
 /*       String password = "12321";
        String md5 = PassWordUtil.toMD5(password);
        System.out.println(md5);*/

        //System.out.println(PassWordUtil.getUUIDPassword());

        System.out.println(PassWordUtil.getRandomWord(7));


    }


    /**
     *
     * @param nums 几位数字的汉字
     * @return 汉字
     */
    public static String getRandomWord(int nums) {
        Random ran = new Random();
        int delta = 0x9fa5 - 0x4e00 + 1;
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<nums;i++){
            sb.append((char)(0x4e00+ran.nextInt(delta)));
        }
        return sb.toString();
    }
}
